import { login, getUserInfo, loginOut } from '@/api/user'
import { getToken, setToken, removeToken, getImgSrc, setImgSrc, removeImgSrc } from '@/utils/auth'
import { resetRouter } from '@/router'
// 导入静态路由
import { constantRoutes } from '@/router'
import { Message } from 'element-ui'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  tokenName: '数字资产',
  userInfo: {
    sysUser: {
      id: ''
    },
    menuList: [],
    permissionList: []
  },
  imgBaseUrl: getImgSrc(),
  routes: constantRoutes
  // imgBaseUrl: 'http://8.141.7.66/static'
  // imgBaseUrl: '/api/erp/static'
}
const mutations = {
  setToken(state, token) {
    state.token = token
    // 同步到缓存
    setToken(token)
  },
  removeToken(state) {
    // 删除Vuex的token
    state.token = null
    removeToken()
    removeImgSrc()
  },
  setName(state, name) {
    state.name = name
  },
  setAvatar(state, avatar) {
    state.avatar = avatar
  },
  setimgBaseUrl(state) {
    state.imgBaseUrl = 'https://oss.yukukeji.com/'
    setImgSrc(state.imgBaseUrl)
  },
  // 定义修改用户信息的方法
  setUserInfo(state, userInfo) {
    state.userInfo.sysUser = userInfo.sysUser
    state.userInfo.menuList = userInfo.menuList
    state.userInfo.permissionList = userInfo.permissionList
  },
  // 修改用户可以访问的路由规则
  setRoutes(state, newRoutes) {
    // 合并静态路由和可以访问的动态路由
    state.routes = [...constantRoutes, ...newRoutes]
  }
  // getRoutes(state, newRoutes) {
  //   // 合并静态路由和可以访问的动态路由
  //   state.routes = [...constantRoutes, ...newRoutes]
  // }
}
const actions = {
  setimgBaseUrl(context) {
    context.commit('setimgBaseUrl')
  },
  // context上下文，传入参数
  async login(context, data) {
    // console.log(data)
    // todo: 调用登录接口
    const res = await login(data)
    if (res.code === 200) {
      console.log(res)
      // 返回一个token 123456
      context.commit('setToken', res.data.token)
      // context.commit('setUserInfo', res.data)
    } else {
      // Message.error(res.message)
      console.log(res.message)
      return res.code
    }
  },
  // 封装调用接口的方法
  async getUserInfo(context) {
    // context.state.userInfo.userId = '12345'
    // 调用接口 返回用户基本信息
    const result = await getUserInfo()
    if (result.code === 200) {
      // console.log(result)
      context.commit('setUserInfo', result.data)
      context.commit('setName', result.data.sysUser.employeeName)
    }
    // 返回用户信息
    return result
  },
  // 退出登录
  async logOut(context) {
    const res = await loginOut()
    if (res.code === 200) {
      // 删除token
      context.commit('removeToken')
      // 删除用户信息
      context.commit('setUserInfo', {
        sysUser: {
          id: ''
        },
        menuList: []
      })
      // 把动态路由清掉
      context.commit('setRoutes', [])
      // 清除路由
      resetRouter()
    } else {
      // Message.error(res.message)
      console.log(res.message)
      return res.code
    }
  },
  async getInfo(context) {
    // const res = await getInfo(context.state.token)
    // console.log(res)
    // context.commit('setName', res.data.name)
    // context.commit('setAvatar', res.data.avatar)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
